<template>
  <div class="dev-tools-container">
    <div class="page-header">
      <h1>开发工具</h1>
      <p>系统开发和调试工具集合</p>
    </div>

    <div class="tools-grid">
      <a-card title="路由信息" :bordered="false" class="tool-card">
        <template #extra>
          <a-button type="link" size="small" @click="showRoutes = !showRoutes">
            {{ showRoutes ? '隐藏' : '查看' }}
          </a-button>
        </template>

        <p>查看当前应用的路由表信息</p>

        <div v-if="showRoutes" class="route-info">
          <a-descriptions size="small" :column="1" bordered>
            <a-descriptions-item label="当前路由">/dev-tools</a-descriptions-item>
            <a-descriptions-item label="路由总数">{{ routeCount }}</a-descriptions-item>
            <a-descriptions-item label="动态路由">已启用</a-descriptions-item>
          </a-descriptions>
        </div>
      </a-card>

      <a-card title="缓存管理" :bordered="false" class="tool-card">
        <p>清理应用缓存数据</p>

        <a-space direction="vertical" style="width: 100%">
          <a-button type="primary" ghost block @click="clearPermissionCache">
            清理权限缓存
          </a-button>

          <a-button type="primary" ghost block @click="clearRouteCache"> 清理路由缓存 </a-button>

          <a-button danger block @click="clearAllCache"> 清理所有缓存 </a-button>
        </a-space>
      </a-card>

      <a-card title="系统信息" :bordered="false" class="tool-card">
        <a-descriptions size="small" :column="1">
          <a-descriptions-item label="Vue版本">{{ vueVersion }}</a-descriptions-item>
          <a-descriptions-item label="环境模式">{{ mode }}</a-descriptions-item>
          <a-descriptions-item label="构建时间">{{ buildTime }}</a-descriptions-item>
        </a-descriptions>
      </a-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { clearAllRouteCaches } from '@/router/dynamic'

const router = useRouter()
const showRoutes = ref(false)
const routeCount = ref(0)
const vueVersion = ref('3.x')
const mode = ref(import.meta.env.MODE)
const buildTime = ref(new Date().toLocaleString())

onMounted(() => {
  routeCount.value = router.getRoutes().length
})

const clearPermissionCache = () => {
  localStorage.removeItem('user_permissions_cache')
  message.success('权限缓存已清理')
}

const clearRouteCache = () => {
  localStorage.removeItem('dynamic_routes_cache')
  localStorage.removeItem('routes_initialized')
  message.success('路由缓存已清理')
}

const clearAllCache = () => {
  clearAllRouteCaches()
  message.success('所有缓存已清理')
}
</script>

<style scoped lang="scss">
.dev-tools-container {
  padding: 24px;
}

.page-header {
  margin-bottom: 24px;

  h1 {
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 8px;
    color: #262626;
  }

  p {
    color: #8c8c8c;
    margin: 0;
  }
}

.tools-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 16px;
}

.tool-card {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);

  :deep(.ant-card-head) {
    border-bottom: 1px solid #f0f0f0;
  }

  :deep(.ant-card-body) {
    padding: 16px;
  }
}

.route-info {
  margin-top: 16px;
}

:deep(.ant-descriptions-item-label) {
  font-weight: 500;
}
</style> 